package com.imooc.controller;

import com.imooc.pojo.Carousel;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.pojo.vo.NewItemsVO;
import com.imooc.service.CarouselService;
import com.imooc.service.CategoryService;
import com.sun.corba.se.spi.ior.IdentifiableFactory;
import imooc.enums.YesOrNo;
import imooc.utils.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(value = "首页", tags = {"首页"})
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/18 10:26
     * @description    查询所有轮播图
     **/
    @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
    @GetMapping("/carousel")
    public ApiResponse setSession(HttpServletRequest request) throws Exception {
        List<Carousel> carouselList = carouselService.queryAll(YesOrNo.YES.type);
        return ApiResponse.ok(carouselList);
    }

    /**
     * 首页分类展示需求
     * 1，第一次刷新主页查询大分类，渲染展示到首页
     * 2，如果鼠标上移到大分类，则加载其子分类的内容
     *
     * */
    @ApiOperation(value = "获取一级分类", notes = "获取一级分类", httpMethod = "GET")
    @GetMapping("/cats")
    public ApiResponse cats(HttpServletRequest request) throws Exception {
        List<Category> categoriesList = categoryService.queryAllRootLevelCat();
        return ApiResponse.ok(categoriesList);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/22 14:35
     * @description    获取商品子分类
     **/
    @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public ApiResponse subCat(
            @ApiParam(name = "rootCatId", value = "一级分类ID",required = true)
            @PathVariable @NotNull Integer rootCatId) throws Exception {
        if (null == rootCatId) {
            return ApiResponse.errorMsg("");
        }
        List<CategoryVO> categoryVOList = categoryService.getSubCatList(rootCatId);
        return ApiResponse.ok(categoryVOList);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/25 11:41
     * @description    查询每个一级分类下的最新6条商品数据
     **/
    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据", notes = "查询每个一级分类下的最新6条商品数据", httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public ApiResponse sixNewItems(
            @ApiParam(name = "rootCatId", value = "一级分类ID",required = true)
            @PathVariable @NotNull Integer rootCatId) throws Exception {
        if (null == rootCatId) {
            return ApiResponse.errorMsg("");
        }
        List<NewItemsVO> categoryVOList = categoryService.getSixNewItemsLazy(rootCatId);
        return ApiResponse.ok(categoryVOList);
    }

}
